﻿using System;
using System.Data;
using DTcms.Common;

namespace DTcms.Web.Mvc.UI.Page
{
    public partial class usercenter : BaseController
    {
        protected string action = string.Empty;
        protected string curr_login_ip = string.Empty;
        protected string pre_login_ip = string.Empty;
        protected string pre_login_time = string.Empty;
        protected int total_order;
        protected int total_msg;
        protected string redirect = string.Empty;//回传地址

        /// <summary>
        /// 重写虚方法,此方法在Init事件执行
        /// </summary>
        public usercenter()
        {
            action = DTRequest.GetQueryString("action");
            var userModel = GetUserInfo();

            //获得最后登录日志
            DataTable dt = new BLL.user_login_log().FillDataSet(2, "user_name='" + userModel.user_name + "'", "id desc").Tables[0];
            if (dt.Rows.Count == 2)
            {
                curr_login_ip = dt.Rows[0]["login_ip"].ToString();
                pre_login_ip = dt.Rows[1]["login_ip"].ToString();
                pre_login_time = dt.Rows[1]["login_time"].ToString();
            }
            else if (dt.Rows.Count == 1)
            {
                curr_login_ip = dt.Rows[0]["login_ip"].ToString();
            }
            //未读短信息
            total_msg = Utils.StrToInt(new BLL.user_message().GetCount("count(*)", "accept_user_name='" + userModel.user_name + "' and is_read=0"));

            //退出登录
            if (action == "exit")
            {
                //清险Session
                System.Web.HttpContext.Current.Session[DTKeys.SESSION_USER_INFO] = null;

                //清除token
                Utils.WriteCookie(DTKeys.COOKIE_USER_TOKEN, "DTcms", -43200);
                //清除Cookies
                Utils.WriteCookie(DTKeys.COOKIE_USER_NAME_REMEMBER, "DTcms", -43200);
                Utils.WriteCookie(DTKeys.COOKIE_USER_PWD_REMEMBER, "DTcms", -43200);
                Utils.WriteCookie("UserName", "DTcms", -1);
                Utils.WriteCookie("Password", "DTcms", -1);
                //自动登录,跳转URL
                redirect = linkurl("login");
            }
        }
    }
}
